<template>
  <div class="com-container">
    <div class="com-chart" ref="rankRef"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      allData: null,
      myChart: null,
      //颜色渐变值
      colorArr: [
        ["#0ba82c", "#4ff778"],
        ["#2e72bf", "#23e5e5"],
        ["#5052ee", "#ab6ee5"],
      ],
      //设置区域缩放的起点和终点
      startValue: 0,
      endValue: 9,
      //定时器标识
      timer: null,
      titleFontSize: 0,
    };
  },
  created() {
    this.$socket.registerCallBack("rankData", this.getData);
  },
  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: "getData",
      socketType: "rankData",
      chartName: "rank",
      value: "",
    });
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timer);
    this.$socket.unRegisterCallBack("rankData");
  },
  computed: {
    ...mapState(["theme"]),
  },
  watch: {
    theme() {
      //销毁当前图表
      this.myChart.dispose();
      //以最新的主题初始化图表对象
      this.initChart();
      //屏幕适配
      this.screenAdapter();
      //渲染数据
      this.updateChart();
    },
  },
  methods: {
    //获取数据
    getData(res) {
      //   const { data: res } = await this.$http.get("rank");
      this.allData = res.sort((a, b) => b.value - a.value);
      this.updateChart();
    },
    //初始化图表
    initChart() {
      this.myChart = this.$echarts.init(this.$refs.rankRef, this.theme);
      const initOption = {
        title: {
          text: "▎地区销售排名",
          top: "5%",
          left: "3%",
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        tooltip: {},
        grid: {
          top: "30%",
          left: "3%",
          right: "3%",
          bottom: "5%",
          containLabel: true,
        },
      };
      this.myChart.setOption(initOption);
      this.startInterval();
      this.myChart.on("mouseover", () => {
        clearInterval(this.timer);
      });
      this.myChart.on("mouseout", () => {
        this.startInterval();
      });
    },
    updateChart() {
      //x轴数据
      const provinceArr = this.allData.map((item) => item.name);
      //y轴数据
      const valueArr = this.allData.map((item) => item.value);

      const dataOption = {
        xAxis: {
          data: provinceArr,
        },
        //区域缩放
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue,
        },
        series: [
          {
            type: "bar",
            data: valueArr,
            //柱子的宽度
            barWidth: this.titleFontSize,
            itemStyle: {
              color: (arg) => {
                let targetColor = null;
                if (arg.value > 300) {
                  targetColor = this.colorArr[0];
                } else if (arg.value > 200) {
                  targetColor = this.colorArr[1];
                } else {
                  targetColor = this.colorArr[2];
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColor[0],
                  },
                  {
                    offset: 1,
                    color: targetColor[1],
                  },
                ]);
              },
              //柱子顶部半圆效果
              // [左上角，右上角，右下角，左下角]
              borderRadius: [
                this.titleFontSize / 2,
                this.titleFontSize / 2,
                0,
                0,
              ],
            },
          },
        ],
      };
      this.myChart.setOption(dataOption);
    },
    //屏幕适配
    screenAdapter() {
      this.titleFontSize = (this.$refs.rankRef.offsetWidth / 100) * 3.1;
      const adaptOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize,
          },
        },
        series: [
          {
            //柱子的宽度
            barWidth: this.titleFontSize,
            //柱子顶部半圆效果
            itemStyle: {
              // [左上角，右上角，右下角，左下角]
              borderRadius: [
                this.titleFontSize / 2,
                this.titleFontSize / 2,
                0,
                0,
              ],
            },
          },
        ],
      };
      this.myChart.setOption(adaptOption);
      this.myChart.resize();
    },
    //设置定时器，
    startInterval() {
      if (this.timer) clearInterval(this.timer);
      if (!this.allData) return;
      this.timer = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue >= this.allData.length) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updateChart();
      }, 2000);
    },
  },
};
</script>